import uvicorn
from fastapi import FastAPI
import uvicorn
from pydantic import BaseModel
from typing import Optional
from enum import Enum

app = FastAPI(
    title="AI0804 API",
    description="FastAPI 描述信息",
    version="0.0.1",
    contact={
        "username": "Luke",
        "email": "1998@qq.com"
    }
)

# deprecated=True 标记接口被弃用
@app.get("/", tags=["Hello world"], description="描述信息", summary="接口描述信息")
def hello_fastapi():
    return {"message": "hello AI0804"}







@app.get("/item")
def hello_item():
    return "Hello --item--  hello ...."

@app.post("/item")
def hello_item():
    return "Hello --item-- Post ...."

# 路径参数
@app.get("/item/{itemid}")
def get_item(itemid: str):
    return {"message": itemid}

# 路径参数(重复)
@app.get("/item/me")
def get_item(itemid: str = None):
    return {"message": itemid}





# 查询参数
@app.get("/items")
def get_item_info(skip: int, limit: int = 10):
    """
        :param skip: 起始页码
        :param limit: 每页的数量
        :return: items中当前页的数据
    """
    return {"message": skip, "limit": skip + limit}



# 请求体
class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: float = 0.0

@app.post("/items/add")
def add_item(item: Item):
    print(item.name, item.price)
    print(item.dict)
    return item

# 定义一个枚举值的类
class OrderStatus(str, Enum):
    ordered = 1001
    order_wait_payment = 1002
    order_paymented = 1003
    order_finished = 1004

@app.get("/items/order")
def order(ost: OrderStatus):
    if ost == OrderStatus.ordered:
        return {"message": "已下单未支付"}
    elif ost == OrderStatus.order_paymented:
        return {"message": "已支付，待发货"}
    elif ost == OrderStatus.order_finished:
        return {"message": "已确认收货"}
    else:
        return {"message": "其它信息"}



if __name__ == '__main__':
    uvicorn.run('hello:app', host='localhost', port=8000, reload=True)